<template lang="pug">
  keep-alive
    kalix-table(bizKey="examresult" title="成绩汇总列表"
      v-bind:targetURL="targetUrl" v-bind:tableFields="tableFields" bizSearch="ExamResultSearch"
      v-bind:btnList="btnList" v-bind:bizDialog="bizDialog" v-bind:toolbarBtnList="toolbarBtnList"
      v-bind:customToolBar="customToolBar" ref="examResultTable")
</template>

<script type="text/ecmascript-6">
  import {ExamResultURL,ExamExportURL} from '../config.toml'
  import {examResultBtnList} from './config'

  export default {
    name: 'kalix-exam-examresult',
    data() {
      return {
        targetUrl: ExamResultURL,
        btnList: examResultBtnList,
        tableFields: [
          {prop: 'examCardNumber', label: '准考证号'},
          {prop: 'name', label: '姓名'},
          {prop: 'idCards', label: '身份证号'},
          {prop: 'subject', label: '科目'},
          {prop: 'totalScore', label: '总成绩'},
          {prop: 'firstTeacher', label: '初阅人'},
          {prop: 'secondTeacher', label: '复阅人'},
          {prop: 'groupLeader', label: '组长'}
        ],
        bizDialog: [
          {id: 'view', dialog: 'ExamResultView'}
        ],
        toolbarBtnList: [
          {id: 'add', isShow: false},
          {id: 'exportExcel', title: '汇总导出', isShow: true, icon: 'icon-add'}
        ]
      }
    },
    methods: {
      customToolBar(btnId, table) {
        switch (btnId) {
          case 'exportExcel': {
            this.onExportExcel(table)
            break
          }
        }
      },
      onExportExcel(table) {
        let queryParams = table.getQueryParams()
        if (queryParams != undefined && queryParams.length > 0) {
          let queryObj = JSON.parse(queryParams)
          let subjectCode = queryObj.subjectVal
          let dateBegin = queryObj.dateBegin
          if (subjectCode == undefined || subjectCode.length == 0
              || dateBegin == undefined || dateBegin.length == 0) {
            this.$KalixMessage.error('请先查询出数据在做导出操作！')
            return
          }
        } else {
          this.$KalixMessage.error('请先查询出汇总数据在做导出操作！')
          return
        }

        const loading = this.$loading({
          lock: true,
          text: 'Excel数据导出中...',
          spinner: 'el-icon-loading',
          background: 'rgba(255, 255, 255, 0.7)'
        })

        this.axios.request({
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          method: 'get',
          baseURL: '../api',
          url: ExamExportURL,
          responseType: 'blob',
          params: {
            jsonStr: queryParams
          }
        }).then(response => {
          if (response.data) {
            this.download(response.data)
            this.$KalixMessage.success('导出成功！')
          } else {
            this.$KalixMessage.error('导出失败！')
          }
          loading.close();
        }).catch((e) => {
          this.$KalixMessage.error('导出失败！原因：' + e.message)
          loading.close();
        })
      },
      download(data) {
        let url = window.URL.createObjectURL(new Blob([data]))
        let link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('download', '汇总成绩.xlsx')
        document.body.appendChild(link)
        link.click()
      }
    }
  }
</script>

<style scoped>

</style>
